package com.seeyon.apps.exportExcelCtrl.units;

import com.alibaba.fastjson.JSONObject;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.log.CtpLogFactory;
import org.apache.commons.lang.time.FastDateFormat;
import org.apache.commons.logging.Log;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

/**
 * @Title ExportExcelCtrlUtil
 * @description: 领导数据导出自定义按钮工具类
 * @author wxt.daicheng
 * @date 2024/8/20 
 * @version 1.0
 */
public class ExportExcelCtrlUtil {

    private static final Log LOG = CtpLogFactory.getLog(ExportExcelCtrlUtil.class);
    //作者
    public static String AUTHOR = "wxt.daicheng";
    //表单编号
    public static String FORM_CODE = AppContext.getSystemProperty("exportExcelCtrl.formCode");
    //权限配置表表单编号
    public static String QX_FORM_CODE = AppContext.getSystemProperty("exportExcelCtrl.qxFormCode");
    /**
     *
     * @author wxt.chenfan
     * @date 2024年5月10日
     * @param content 日志内容
     * @param operateName 日志所属功能的名称
     * @param e
     */
    public static void log(String content,String operateName, Exception e){
        if(e == null){
            LOG.info("---" + AUTHOR + "---" + operateName + "---"  + content);
        }else{
            LOG.error("---" + AUTHOR + "---" + operateName + "---" + content, e);
        }
    }

    /**
     *
     * <p>Title: buildMsg
     * <p>Description: 返回值规范
     * @author wxt.daicheng
     * @date 2024年5月10日
     * @param code 状态码
     * @param msg 信息
     * @param data 数据
     * @return
     */
    public static JSONObject buildMsg(Integer code, String msg, Object data) {
        JSONObject json = new JSONObject();
        json.put("code", code);
        json.put("msg", msg);
        json.put("data", data);
        return json;
    }

    /**
     * Title: download
     * Description: 文件下载工具
     * @author: wxt.daicheng
     * @date: 2023/6/1
     * @param: response response对象
     * @param: filePath 文件路径
     * @param: fileName 文件名称
     * @param: encode 文件编码
     * @return: void
     */
    public static void download(HttpServletResponse response, String filePath,String fileName, String encode) {
        TimeZone GMT=TimeZone.getTimeZone("Asia/Shanghai");
        FastDateFormat RFC1123=FastDateFormat.getInstance("EEE, dd MMM yyyy HH:mm:ss zzz",GMT, Locale.US);

        response.setContentType("text/html;charset=" + encode);
        File file = new File(filePath);
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            bis = new BufferedInputStream(Files.newInputStream(Paths.get(filePath)));
            bos = new BufferedOutputStream(response.getOutputStream());
            long fileLength = file.length();
            response.setContentType("application/x-msdownload;");

            response.setHeader("Content-disposition", "attachment; filename=" + new String(fileName.getBytes(), StandardCharsets.ISO_8859_1));

            response.setHeader("Last-Modified", RFC1123.format(new Date(file.lastModified())));
            response.setHeader("Content-Length", String.valueOf(fileLength));
            byte[] buff = new byte[2048];
            int bytesRead;
            while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
                bos.write(buff, 0, bytesRead);
            }
        } catch (Exception e) {
            log("文件下载失败！","数据导出",e);
        }finally {
            try {
                if (bis != null){
                    bis.close();
                }
                if (bos != null){
                    bos.close();
                }
            } catch (IOException e) {
                log("关闭流失败！","数据导出",e);
            }
        }
    }

}
